dojo.provide("miaigo.model.sgf.properties.SimpleText");

dojo.require("miaigo.model.sgf.SGFProperty");

dojo.declare(
    "miaigo.model.sgf.properties.SimpleText",
    [miaigo.model.sgf.SGFProperty],
    {
	onApply: function(game)
	{
	    var value = this.getValue();

	    if ("RE" == this.getID())
	    {
		game.setResult(this._parseValueAsResult(value));
	    }
	    else
	    {
		switch (this.getID())
		{
		case "EV":
		    game.setInfo("event", value);
		    break;
		case "DT":
		    game.setInfo("date", value);
		    break;
		case "US":
		    game.setInfo("author", value);
		    break;
		case "SO":
		    game.setInfo("source", value);
		    break;
		case "PC":
		    game.setInfo("location", value);
		    break;
		case "PB":
		    game.setInfo("black-player", value);
		    break;
		case "BR":
		    game.setInfo("black-rank", value);
		    break;
		case "PW":
		    game.setInfo("white-player", value);
		    break;
		case "WR":
		    game.setInfo("white-rank", value);
		    break;
		}
	    }
	},

	getValue: function(/*integer (optional)*/index)
	{
	    return this.getRawValue(index);
	},

	/*
	 * "0" (zero) or "Draw" for a draw (jigo),
	 * "B+" ["score"] for a black win and
	 * "W+" ["score"] for a white win
	 * Score is optional (some games don't have a score e.g. chess).
         * If the score is given it has to be given as a real value,
         * e.g. "B+0.5", "W+64", "B+12.5"
         * Use "B+R" or "B+Resign" and "W+R" or "W+Resign" for a win by
         * resignation. Applications must not write "Black resigns".
         * Use "B+T" or "B+Time" and "W+T" or "W+Time" for a win on time,
         * "B+F" or "B+Forfeit" and "W+F" or "W+Forfeit" for a win by
         * forfeit,
         * "Void" for no result or suspended play and
         * "?" for an unknown result.
         */
	_parseValueAsResult: function(value)
	{
	    /* Return format:
             *   {
             *     winner: null | "black" | "white"
             *     score: null | <Number>,
             *     reason: null | "resign" | "forfeit" | "time" | "draw" | "suspended"
             *   }
             */
	    var result =
		{
		    winner: null,
		    score: null,
		    reason: null
		};

	    if (value == "Void")
	    {
		result.reason = "suspended";
	    }
	    else if (value == "0" || value == "Draw")
	    {
		result.score = 0;
		result.reason = "draw";
	    }
	    else if (value.indexOf("+") == 1)
	    {
		if (value.charAt(0) == 'B')
		{
		    result.winner = "black";
		}
		else if (value.charAt(0) == 'W')
		{
		    result.winner = "white";
		}

		if (result.winner)
		{
		    var scoreOrReason = value.substring(2);
		    var score = new Number(scoreOrReason);

		    if (!isNaN(score))
		    {
			result.score = score;
		    }
		    else
		    {
			switch(scoreOrReason)
			{
			case "R":
			case "Resign":
			    result.reason = "resign";
			    break;
			case "T":
			case "Time":
			    result.reason = "time";
			    break;
			case "F":
			case "Forfeit":
			    result.reason = "forfeit";
			    break;
			}
		    }
		}
	    }

	    return result;
	}
    });